<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    protected $fillable=['name','carry_user_id','status','count','create_user_id','start_time','end_time'];

//    public $timestamps = false;

    public function createUser(){
        return $this->belongsTo(User::class,'create_user_id','id');
    }
    public function carryUser(){
        return $this->belongsTo(User::class,'carry_user_id','id');
    }
    public function fsUsers(){
        $db1 = env('DB_DATABASE');
        return $this->belongsToMany(FsUser::class,"{$db1}.fs_user_tasks", 'task_id', 'fs_user_id')->withPivot('status');
    }
    public function fsUserTask(){
        return $this->hasMany(FsUserTask::class);
    }
    public function phoneRecord(){
        return $this->hasMany(PhoneRecord::class);
    }


    //当前登陆者任务个数
    public function search($type ,$carry_id,$limit = 10){
        $builder = self::query();
        if($carry_id){
            $builder->where('carry_user_id',$carry_id);
        }

        if($type == 1){
            $time = weekTime();
            $builder->where(function ($query) use ($time) {
                $query->where('start_time','>',$time['week_start']);
                $query->orWhere('end_time','<',$time['week_end']);
            });
        }elseif ($type == 2){
            $time = getMonth();
            $builder->where(function ($query) use ($time) {
                $query->where('start_time','>',$time['month_start']);
                $query->orWhere('end_time','<',$time['month_end']);
            });
        }
//        return $builder->count();
        return $builder->paginate($limit);
    }
}
